/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

import appClient.ClientCommand;
import clientListener.ClientEvent;
import clientListener.ClientListener;
import appClient.ConnectClient;
import appClient.UserComman;
import com.google.gson.Gson;
import engine.Persone;
import java.util.List;
import com.google.gson.reflect.TypeToken;
import engine.City;
import engine.Country;
import engine.FullPersone;
import engine.Singleton;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import java.lang.reflect.Type;
import protocol.ClientProtocol;

/**
 *
 * @author User
 */
public class WebClientListener implements ClientListener {

  @Override
  public String setRole(ClientEvent evn) {
      String status = "";
    ConnectClient connectClient = evn.connectClient;
    String role = evn.data;
    String add = evn.add;
    if (role.equals("Error")){
      System.out.println("not login");
      status = "Error";
    }
    if (role.equals("Admin")){
      Singleton.role = "Admin";
      Singleton.Uid = add;
      try {
        UserComman.getList(connectClient);
      } catch (Exception exp)
      {
        System.out.println("No Server");
      }
      status = "Admin";
    }
    if (role.equals("SUser")){
      Singleton.role = "SUser";
      Singleton.Uid = add;
      try {
        UserComman.getList(connectClient);
      } catch (Exception exp)
      {
        System.out.println("No Server");
      }
      status = "SUser";
    }
    if (role.equals("User")){
      Singleton.role = "User";
      Singleton.Uid = add;
      try {
        UserComman.getList(connectClient);
      } catch (Exception exp)
      {
        System.out.println("No Server");
      }
      status = "User";
    }
    return status;
  }

  @Override
  public boolean setList(ClientEvent evn) {
    ConnectClient connectClient = evn.connectClient;
    String add = evn.add;
    Gson gson = new Gson();
    Type fooType = new TypeToken<List<Persone>>() {}.getType();
    List<Persone> list = gson.fromJson(add, fooType);

    WebFunction.setTable(list, connectClient);
    return true;
  }

  @Override
  public boolean loadPersone(ClientEvent evn) {
      FullPersone fp = evn.fp;
      ClientProtocol cp = evn.cp;
      ConnectClient connectClient = evn.connectClient;
      Singleton.fp = fp;
     // cp.ab = new AdressBooks(fp, cp);
      ClientCommand.loadPT(cp, connectClient);
      return true;
  }

  @Override
  public boolean typePhone(ClientEvent evn) {
     // Forms ab = evn.ab;
      String types = evn.data;
      Gson gson = new Gson();
      Type fooType = new TypeToken<List<TypePhone>>() {
      }.getType();
      List<TypePhone> list = gson.fromJson(types, fooType);
      Singleton.list = list;

     // ab.setItems(phoneT);
     // ab.phoneTable();

      return true;
  }

  @Override
  public boolean typeAdr(ClientEvent evn) {
    //  Forms ab = evn.ab;
      String add = evn.data;
      Gson gson = new Gson();
      Type fooType = new TypeToken<List<TypeAdress>>() {
      }.getType();
      List<TypeAdress> list = gson.fromJson(add, fooType);
      Singleton.typeAdr = list;

//      String phoneT[] = new String[list.size()];
//      int count = 0;
//      for (TypeAdress p : list) {
//          phoneT[count] = p.Type;
//          count++;
//      }
    //  ab.setAdrType(phoneT);
    //  ab.AdrTable();

      return true;
  }

  @Override
  public boolean country(ClientEvent evn) {
    //  Forms ab = evn.ab;
      String add = evn.data;
      Gson gson = new Gson();
      Type fooType = new TypeToken<List<Country>>() {
      }.getType();
      List<Country> list = gson.fromJson(add, fooType);
      Singleton.country = list;

//      String phoneT[] = new String[list.size()];
//      int count = 0;
//      for (Country p : list) {
//          phoneT[count] = p.name;
//          count++;
//      }
    //  ab.setCountr(phoneT);
    //  ab.setCountryList(list);
    //  ab.AdrTable();

      return true;
  }

  @Override
  public boolean city(ClientEvent evn) {
     // Forms ab = evn.ab;
      String add = evn.data;
      Gson gson = new Gson();
      Type fooType = new TypeToken<List<City>>() {
      }.getType();
      List<City> list = gson.fromJson(add, fooType);
      Singleton.city = list;

//      String phoneT[] = new String[list.size()];
//      int count = 0;
//      for (City p : list) {
//          phoneT[count] = p.name;
//          count++;
//      }
      //ab.setCit(phoneT);
      //ab.setCityList(list);
      //ab.setTableAdr();
      // ab.AdrTable();

      return true;
  }

  @Override
  public boolean street(ClientEvent evn) {
  
    String add = evn.data;
        Gson gson = new Gson();       
        Type fooType = new TypeToken<List<Street>>() {}.getType();
        List<Street> list = gson.fromJson(add, fooType);
        Singleton.street = list;
        
//        String phoneT[] = new String[list.size()];
//        int count = 0;
//        for (Street p : list) {
//            phoneT[count] = p.name;
//            count++;
//        }
        

        return true;
  }

    @Override
    public boolean foto(ClientEvent clientEvent) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
  
}
